﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Runtime.Serialization;
using System.Globalization;

namespace RESTServices.Dominio
{
    [DataContract]
    public class Itinerario
    {
        [DataMember]
        public int Id_Itinerario { get; set; }

        [DataMember]
        public int Id_Origen { get; set; }

        [DataMember]
        public string Descripcion_Origen { get; set; }

        [DataMember]
        public int Id_Destino { get; set; }

        [DataMember]
        public string Descripcion_Destino { get; set; }

        public DateTime Salida { get; set; }

        public DateTime Llegada { get; set; }

        [DataMember]
        public int Total_Asiento { get; set; }

        [DataMember]
        public string Estado { get; set; }

        [DataMember(Name = "Salida")]
        private string FSalida;

        [DataMember(Name = "Llegada")]
        private string Fllegada;

        [OnSerializing]
        void OnSerializing(StreamingContext context)
        {
            this.FSalida = this.Salida.ToString("yyyy-MM-dd HH:ss", CultureInfo.InvariantCulture);
            this.Fllegada = this.Llegada.ToString("yyyy-MM-dd HH:ss", CultureInfo.InvariantCulture);
        }

        [OnDeserializing]
        void OnDeserializing(StreamingContext context)
        {
            this.FSalida = "1900-01-01";
            this.Fllegada = "1900-01-01";
        }

        [OnDeserialized]
        void OnDeserialized(StreamingContext context)
        {
            try
            {
                this.Salida = DateTime.ParseExact(this.FSalida, "yyyy-MM-dd HH:ss", CultureInfo.InvariantCulture);
            }
            catch (System.FormatException fEx)
            {
                this.Salida = DateTime.Parse(this.FSalida);
            }
            
            try
            {
                this.Llegada = DateTime.ParseExact(this.Fllegada, "yyyy-MM-dd HH:ss", CultureInfo.InvariantCulture);
            }
            catch (System.FormatException fEx)
            {
                this.Llegada = DateTime.Parse(this.Fllegada);
            }
        }
    }
}